/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Ashe Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/NoteBooks/
 * @date:        2020/2/23 10:44 下午
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.yi.yier.boot.autoconfigure.codegenerator.plugins;

import lombok.Getter;
import lombok.Setter;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Properties;

import static org.mybatis.generator.internal.util.StringUtility.isTrue;

/**
 * <p>
 *
 * </p>
 *
 * @author Yiyuery
 * @version V1.0.0
 * @date 2020/2/23 10:44 下午
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人}  2020/2/23
 * @modify reason: {方法名}:{原因}
 * ...
 */
@Getter
@Setter
public class XPlugin extends PluginAdapter {

    private boolean suppressLombok;

    private boolean suppressSwagger;

    @Override
    public void setProperties(Properties properties) {
        super.setProperties(properties);
        suppressLombok = isTrue(properties.getProperty("suppressLombok")); //$NON-NLS-1$
        suppressSwagger = isTrue(properties.getProperty("suppressSwagger")); //$NON-NLS-1$
    }

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        if(!isSuppressLombok()){
            //添加domain的import
            topLevelClass.addImportedType("lombok.Getter");
            topLevelClass.addImportedType("lombok.Setter");
            topLevelClass.addImportedType("lombok.Builder");
            topLevelClass.addImportedType("lombok.NoArgsConstructor");
            topLevelClass.addImportedType("lombok.AllArgsConstructor");
            //添加domain的注解
            topLevelClass.addAnnotation("@Getter");
            topLevelClass.addAnnotation("@Setter");
            topLevelClass.addAnnotation("@Builder");
            topLevelClass.addAnnotation("@NoArgsConstructor");
            topLevelClass.addAnnotation("@AllArgsConstructor");
        }

        if(!isSuppressSwagger()){
            //添加domain的import
            topLevelClass.addImportedType("io.swagger.annotations.ApiModel");
            topLevelClass.addImportedType("io.swagger.annotations.ApiModelProperty");
            //添加domain的注解
            StringBuffer sb = new StringBuffer();
            sb.append("@ApiModel(value=\"").append(topLevelClass.getType().getShortName()).append
                    ("\",description=\"数据库表：").append
                    (introspectedTable.getFullyQualifiedTable())
                    .append("\")");
            topLevelClass.addAnnotation(sb.toString());
        }
        return true;
    }

    @Override
    public boolean modelSetterMethodGenerated(org.mybatis.generator.api.dom.java.Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        return isSuppressLombok();
    }

    @Override
    public boolean modelGetterMethodGenerated(org.mybatis.generator.api.dom.java.Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        return isSuppressLombok();
    }
}
